<?php

namespace App\Http\Controllers;

class BaseController extends Controller
{
    /**
     * @desc 统一数据输出格式
     *
     * @param  mixed                         $data
     * @param                                $message
     * @return \Illuminate\Http\JsonResponse
     */
    public function success($data = [], $message = 'success')
    {
        $ret = [
            'code' => 0,
            'message' => $message,
            'data' => $data,
        ];

        return response()->json($ret)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }

    /**
     * @desc 统一错误数据输出格式
     *
     * @param                                $code
     * @param                                $message
     * @param  array|object                  $data
     * @return \Illuminate\Http\JsonResponse
     */
    public function error($code, $message = 'fail', $data = [])
    {
        $ret = [
            'code' => $code,
            'message' => $message,
            'data' => $data,
        ];

        return response()->json($ret)->setEncodingOptions(JSON_UNESCAPED_UNICODE);
    }
}
